#!/opt/python-2.5/bin/python2.5

import cgi, cgitb
cgitb.enable()
form = cgi.FieldStorage()
print "Content-type: text/html;charset=utf-8"
print 
print "<body bgcolor=333366 text=E4E4E4 <title>Revelator Demo Player Sign-in</title>"
print 
roll = dict()
namefile = "tempdir/players.file"
file = open(namefile, 'r')
if "Delete" in form.keys():
	delname = form["Delete"].value
else:
	delname = None
for entry in file:
	if entry[:-1] != delname:	roll[entry[:-1]] = "present"
	file = open(namefile, 'w')
	for person in roll:	file.write(person + "\n")
	file.close()
file.close()
print "<center>"
print "<h1><font face=\"Trajan Pro\" size=7 color=DDFFDD>"
print "Welcome to Revelator!"
print "</font></h1>"
print "<form name=\"Handler\" method=get>"
print "<font face=Times size=4><center>"
print "<b>Players already involved in the current game:<br/></font>"
print "<font face=Courier size=3><ul>"
for player in roll:	print "<li>" + player + "<br/>"
print "</ul></center></font><br/></b>"
if ("Name" in form.keys()):
	name = form["Name"].value
	if name not in roll:
		file = open(namefile, 'w')
		for person in roll:	file.write(person + "\n")
		file.write(name + "\n")
		file.close()
		print "<a href=\"Tutor.cgi?Player=" + name + "\" target=_self>"
		print "<button type=submit/>Go boldly onward, " + name + "...</button>"
else:
	print "<h2 align=center><font face=Times size=5 color=\"white\">Please select a unique player handle.</font></h2>"
	print "<button type=reset>Clear</button>\t"
	print "<input name=\"Name\" type=text size=25 maxlength=20 autocomplete/>\t"
	print "<input name=\"Delete\" type=hidden />"
	print "<button type=submit/>Enter</button></a><br/>"
if ("Name" in form.keys()):
	if name in roll:
		print "<h4 align=center>"
		print "<font color=FF3333 size=4 face=Arial><i>I'm sorry, but that handle is already taken.  Please choose another one."
		print "<br/>"
		print "</i></font><br/>"
		print "<a href=\"startdemo.cgi\" target=_self>"
		print "<button type=button>Go back to the sign-in page.</button></a>"
print "</form>"
print "</center>"
print "</body>"
